package com.atguigu.controller;

import com.alibaba.druid.util.StringUtils;
import com.atguigu.entity.UserInfo;
import com.atguigu.result.Result;
import com.atguigu.result.ResultCodeEnum;
import com.atguigu.service.UserInfoService;
import com.atguigu.util.MD5;
import com.atguigu.vo.LoginVo;
import com.atguigu.vo.RegisterVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/userInfo")
@ResponseBody
public class UserInfoController {

    @DubboReference
    private UserInfoService userInfoService;

    @RequestMapping("/sendCode/{phone}")
    public Result sendCode(@PathVariable String phone, HttpSession session) {
        String code = "8888";
        session.setAttribute("phone", code);
        return Result.ok(code);
    }

    @RequestMapping("/register")
    public Result register(@RequestBody RegisterVo registerVo, HttpSession session) {
        String code = registerVo.getCode();
        String nickName = registerVo.getNickName();
        String phone = registerVo.getPhone();
        String password = registerVo.getPassword();
        //非空验证
        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(nickName) || StringUtils.isEmpty(password) || StringUtils.isEmpty(phone)) {
            return Result.build(null, ResultCodeEnum.PARAM_ERROR);
        }
        //手机号验证
        UserInfo userInfo = userInfoService.findByPhone(phone);
        if (userInfo != null) {
            return Result.build(null, ResultCodeEnum.PHONE_REGISTER_ERROR);
        }
        //验证码验证
        String trueCode = (String) session.getAttribute("phone");
        if (trueCode != null) {
            if (!code.equals(trueCode)) {
                return Result.build(null, ResultCodeEnum.CODE_ERROR);
            }
        } else {
            return Result.build(null, ResultCodeEnum.CODE_ERROR);
        }
        //数据添加
        UserInfo info = new UserInfo();
        info.setNickName(nickName);
        info.setPhone(phone);
        info.setPassword(MD5.encrypt(password));
        userInfoService.insert(info);
        return Result.ok();
    }

    @RequestMapping("/login")
    public Result login(@RequestBody LoginVo loginVo, HttpSession session) {
        //非空验证
        if (StringUtils.isEmpty(loginVo.getPhone()) || StringUtils.isEmpty(loginVo.getPassword())) {
            return Result.build(null, ResultCodeEnum.PARAM_ERROR);
        }
        //验证手机号是否注册
        UserInfo userInfo = userInfoService.findByPhone(loginVo.getPhone());
        if (userInfo == null) {
            return Result.build(null, ResultCodeEnum.ACCOUNT_ERROR);
        }
        //验证密码
        if (!userInfo.getPassword().equals(MD5.encrypt(loginVo.getPassword()))) {
            return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
        }
        //验证是否锁定
        if (userInfo.getStatus() != 1) {
            return Result.build(null, ResultCodeEnum.ACCOUNT_LOCK_ERROR);
        }
        //将手机号和用户名返回给前端
        Map map = new HashMap();
        map.put("phone", userInfo.getPhone());
        map.put("nickName", userInfo.getNickName());
        //将数据存入session中
        session.setAttribute("userInfo", userInfo);
        return Result.ok(map);
    }

    @RequestMapping("/logout")
    public Result logout(HttpSession session) {
        session.removeAttribute("userInfo");
        return Result.ok();
    }
}
